home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_08 / phillip2 / txtrsubs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-13  |  24.8 KB  |  928 lines

  1.  
  2.     /***********************************************
  3.     *
  4.     *    file d:\cips\txtrsubs.c
  5.     *
  6.     *    Functions: This file contains
  7.     *       sigma
  8.     *       skewness
  9.     *       amean
  10.     *       adifference
  11.     *       difference_array
  12.     *       hurst
  13.     *       compare
  14.     *       get_texture_options
  15.     *
  16.     *    Purpose:
  17.     *       These functions calculate measures
  18.     *       that help distinguish textures.
  19.     *
  20.     *    External Calls:
  21.     *       wtiff.c - round_off_image_size
  22.     *                 create_file_if_needed
  23.     *                 write_array_into_tiff_image
  24.     *       tiff.c - read_tiff_header
  25.     *       rtiff.c - read_tiff_image
  26.     *       edge.c - fix_edges
  27.     *       fitt.c - fit
  28.     *       filter.c - sort_elements
  29.     *
  30.     *    Modifications:
  31.     *       12 August 1993- created
  32.     *
  33.     *************************************************/
  34.  
  35. #include "cips.h"
  36.  
  37.  
  38.  
  39.      /*******************************************
  40.      *
  41.      *   sigma(..
  42.      *
  43.      *   This calculates the variance and the 
  44.      *   sigma for a sizeXsize area.
  45.      *
  46.      *   It sums the squares of the difference
  47.      *   between each pixel and the mean of
  48.      *   the area and divides that by the
  49.      *   number of pixels in the area.
  50.      *
  51.      *   The output image is set to the square
  52.      *   root of the variance since the variance
  53.      *   will almost certainly be out of range
  54.      *   for the image.  The square root of the
  55.      *   variance will be sigma.
  56.      *
  57.      *******************************************/
  58.  
  59. sigma(in_name, out_name, the_image, out_image,
  60.       il, ie, ll, le, size, threshold, high)
  61.    char   in_name[], out_name[];
  62.    int    il, ie, ll, le,
  63.           high, threshold, size;
  64.    short  the_image[ROWS][COLS],
  65.           out_image[ROWS][COLS];
  66. {
  67.    int      a, b, count, i, j, k,
  68.             max, mean, new_hi, new_low,
  69.             sd2, sd2p1;
  70.    short    sigma;
  71.    struct   tiff_header_struct image_header;
  72.    unsigned long diff, variance;
  73.  
  74.    sd2   = size/2;
  75.    sd2p1 = sd2 + 1;
  76.  
  77.    create_file_if_needed(in_name, out_name, out_image);
  78.  
  79.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  80.  
  81.    max     = 255;
  82.    new_hi  = 250;
  83.    new_low = 16;
  84.    if(image_header.bits_per_pixel == 4){
  85.        new_hi  = 10;
  86.        new_low = 3;
  87.        max     = 16;
  88.    }
  89.  
  90.       /***************************
  91.       *
  92.       *   Loop over image array
  93.       *
  94.       ****************************/
  95.  
  96.    printf("\n");
  97.    for(i=sd2; i<ROWS-sd2; i++){
  98.       if( (i%10) == 0) printf("%d ", i);
  99.       for(j=sd2; j<COLS-sd2; j++){
  100.  
  101.             /*****************************
  102.             *
  103.             *   Run through the small area
  104.             *   and calculate the mean.
  105.             *
  106.             ******************************/
  107.  
  108.          mean = 0;
  109.          for(a=-sd2; a<sd2p1; a++){
  110.             for(b=-sd2; b<sd2p1; b++){
  111.                mean = mean + the_image[i+a][j+b];
  112.             }
  113.          }
  114.          mean = mean/(size*size);
  115.  
  116.             /*****************************
  117.             *
  118.             *   Run through the small area
  119.             *   again and the calculate the
  120.             *   variance.
  121.             *
  122.             ******************************/
  123.  
  124.          variance = 0;
  125.          diff     = 0;
  126.          for(a=-sd2; a<sd2p1; a++){
  127.             for(b=-sd2; b<sd2p1; b++){
  128.                diff     = the_image[i+a][j+b] - mean;
  129.                variance = variance + (diff*diff);
  130.             }
  131.          }
  132.  
  133.          variance = variance/(size*size);
  134.          sigma    = sqrt(variance);
  135.          if(sigma > max) sigma = max;
  136.          out_image[i][j] = sigma;
  137.  
  138.       }  /* ends loop over j */
  139.    }  /* ends loop over i */
  140.  
  141.  
  142.      /* if desired, threshold the output image */
  143.    if(threshold == 1){
  144.        for(i=0; i<ROWS; i++){
  145.           for(j=0; j<COLS; j++){
  146.              if(out_image[i][j] > high){
  147.                   out_image[i][j] = new_hi;
  148.              }
  149.              else{
  150.                   out_image[i][j] = new_low;
  151.              }
  152.           }
  153.        }
  154.    }  /* ends if threshold == 1 */
  155.  
  156.    fix_edges(out_image, sd2);
  157.  
  158.    write_array_into_tiff_image(out_name, out_image,
  159.                                il, ie, ll, le);
  160.  
  161.  
  162. }  /* ends sigma */
  163.  
  164.  
  165.  
  166.  
  167.  
  168.      /*******************************************
  169.      *
  170.      *   skewness(..
  171.      *
  172.      *   This calculates the skewness for a
  173.      *   sizeXsize area.
  174.      *
  175.      *   Look at Levine's book page 449 for
  176.      *   the formula.
  177.      *   "Vision in Man and Machine" by
  178.      *   Martin D. Levine, McGraw Hill, 1985.
  179.      *
  180.      *******************************************/
  181.  
  182. skewness(in_name, out_name, the_image, out_image,
  183.          il, ie, ll, le, size, threshold, high)
  184.    char   in_name[], out_name[];
  185.    int    il, ie, ll, le,
  186.           high, threshold, size;
  187.    short  the_image[ROWS][COLS],
  188.           out_image[ROWS][COLS];
  189. {
  190.    int      a, b, count, i, j, k,
  191.             max, mean, new_hi, new_low,
  192.             sd2, sd2p1;
  193.    long     cube;
  194.    short    sigma, skew;
  195.    struct   tiff_header_struct image_header;
  196.    unsigned long diff, sigma3, variance;
  197.  
  198.    sd2   = size/2;
  199.    sd2p1 = sd2 + 1;
  200.  
  201.    create_file_if_needed(in_name, out_name, out_image);
  202.  
  203.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  204.  
  205.    max     = 255;
  206.    new_hi  = 250;
  207.    new_low = 16;
  208.    if(image_header.bits_per_pixel == 4){
  209.        new_hi  = 10;
  210.        new_low = 3;
  211.        max     = 16;
  212.    }
  213.  
  214.       /***************************
  215.       *
  216.       *   Loop over image array
  217.       *
  218.       ****************************/
  219.  
  220.    printf("\n");
  221.    for(i=sd2; i<ROWS-sd2; i++){
  222.       if( (i%10) == 0) printf("%d ", i);
  223.       for(j=sd2; j<COLS-sd2; j++){
  224.  
  225.             /*****************************
  226.             *
  227.             *   Run through the small area
  228.             *   and calculate the mean.
  229.             *
  230.             ******************************/
  231.  
  232.          mean = 0;
  233.          for(a=-sd2; a<sd2p1; a++){
  234.             for(b=-sd2; b<sd2p1; b++){
  235.                mean = mean + the_image[i+a][j+b];
  236.             }
  237.          }
  238.          mean = mean/(size*size);
  239.  
  240.             /*****************************
  241.             *
  242.             *   Run through the small area
  243.             *   again and the calculate the
  244.             *   variance and the cube.
  245.             *
  246.             ******************************/
  247.  
  248.          variance = 0;
  249.          diff     = 0;
  250.          cube     = 0;
  251.          for(a=-sd2; a<sd2p1; a++){
  252.             for(b=-sd2; b<sd2p1; b++){
  253.                diff     = the_image[i+a][j+b] - mean;
  254.                cube     = cube + (diff*diff*diff);
  255.                variance = variance + (diff*diff);
  256.             }
  257.          }
  258.  
  259.          variance        = variance/(size*size);
  260.          sigma           = sqrt(variance);
  261.          sigma3          = sigma*sigma*sigma;
  262.          if(sigma3 == 0)
  263.             sigma3       = 1;
  264.          skew            = cube/(sigma3*size*size);
  265.          out_image[i][j] = skew;
  266.          if(out_image[i][j] > max)
  267.             out_image[i][j] = max;
  268.  
  269.       }  /* ends loop over j */
  270.    }  /* ends loop over i */
  271.  
  272.  
  273.  
  274.      /* if desired, threshold the output image */
  275.    if(threshold == 1){
  276.        for(i=0; i<ROWS; i++){
  277.           for(j=0; j<COLS; j++){
  278.              if(out_image[i][j] > high){
  279.                   out_image[i][j] = new_hi;
  280.              }
  281.              else{
  282.                   out_image[i][j] = new_low;
  283.              }
  284.           }
  285.        }
  286.    }  /* ends if threshold == 1 */
  287.  
  288.    fix_edges(out_image, sd2);
  289.  
  290.    write_array_into_tiff_image(out_name, out_image,
  291.                                il, ie, ll, le);
  292.  
  293.  
  294. }  /* ends skewness */
  295.  
  296.  
  297.  
  298.  
  299.  
  300.      /*******************************************
  301.      *
  302.      *   amean(..
  303.      *
  304.      *   This calculates the mean measure
  305.      *   for a sizeXsize area.
  306.      *
  307.      *   Look at Levine's book page 451 for
  308.      *   the formula.
  309.      *   "Vision in Man and Machine" by
  310.      *   Martin D. Levine, McGraw Hill, 1985.
  311.      *
  312.      *******************************************/
  313.  
  314. amean(in_name, out_name, the_image, out_image,
  315.           il, ie, ll, le, size)
  316.    char   in_name[], out_name[];
  317.    int    il, ie, ll, le,
  318.           size;
  319.    short  the_image[ROWS][COLS],
  320.           out_image[ROWS][COLS];
  321. {
  322.    int      a, b, count, i, j, k, max,
  323.             sd2, sd2p1;
  324.    short    pixel;
  325.    struct   tiff_header_struct image_header;
  326.    unsigned long big;
  327.  
  328.    sd2   = size/2;
  329.    sd2p1 = sd2 + 1;
  330.  
  331.    create_file_if_needed(in_name, out_name, out_image);
  332.  
  333.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  334.  
  335.    max = 255;
  336.    if(image_header.bits_per_pixel == 4){
  337.        max = 16;
  338.    }
  339.  
  340.       /**************************************
  341.       *
  342.       *   Calculate the gray level difference
  343.       *   array.
  344.       *
  345.       ***************************************/
  346.  
  347.    difference_array(the_image, out_image, size);
  348.    for(i=0; i<ROWS; i++)
  349.       for(j=0; j<COLS; j++)
  350.          the_image[i][j] = out_image[i][j];
  351.  
  352.       /**************************************
  353.       *
  354.       *   Loop over the image array and
  355.       *   calculate the mean measure.
  356.       *
  357.       ***************************************/
  358.  
  359.    printf("\n");
  360.    for(i=sd2; i<ROWS-sd2; i++){
  361.       if( (i%10) == 0) printf("%d ", i);
  362.       for(j=sd2; j<COLS-sd2; j++){
  363.  
  364.          pixel = 0;
  365.          for(a=-sd2; a<sd2p1; a++){
  366.             for(b=-sd2; b<sd2p1; b++){
  367.                pixel = pixel + the_image[i+a][j+b];
  368.             }
  369.          }
  370.       out_image[i][j] = pixel/(size*size);
  371.       if(out_image[i][j] > max)
  372.          out_image[i][j] = max;
  373.  
  374.       }  /* ends loop over j */
  375.    }  /* ends loop over i */
  376.  
  377.    fix_edges(out_image, sd2);
  378.  
  379.    write_array_into_tiff_image(out_name, out_image,
  380.                                il, ie, ll, le);
  381.  
  382. }  /* ends amean */
  383.  
  384.  
  385.  
  386.  
  387.  
  388.      /*******************************************
  389.      *
  390.      *   adifference(..
  391.      *
  392.      *   This function performs the difference
  393.      *   operation for a specified array
  394.      *   in an image file.
  395.      *
  396.      *******************************************/
  397.  
  398. adifference(in_name, out_name, the_image, out_image,
  399.             il, ie, ll, le, size)
  400.    char   in_name[], out_name[];
  401.    int    il, ie, ll, le,
  402.           size;
  403.    short  the_image[ROWS][COLS],
  404.           out_image[ROWS][COLS];
  405. {
  406.    int      sd2, sd2p1;
  407.    struct   tiff_header_struct image_header;
  408.  
  409.    sd2   = size/2;
  410.    sd2p1 = sd2 + 1;
  411.  
  412.    create_file_if_needed(in_name, out_name, out_image);
  413.  
  414.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  415.  
  416.    difference_array(the_image, out_image, size);
  417.  
  418.    fix_edges(out_image, sd2);
  419.  
  420.    write_array_into_tiff_image(out_name, out_image,
  421.                                il, ie, ll, le);
  422.  
  423. }  /* ends adifference */
  424.  
  425.  
  426.  
  427.  
  428.  
  429.      /*******************************************
  430.      *
  431.      *   difference_array(..
  432.      *
  433.      *   This function takes the input image
  434.      *   array the_image and places in out_image
  435.      *   the gray level differences of the pixels
  436.      *   in the_image.  It uses the size
  437.      *   parameter for the distance between pixels
  438.      *   used to get the difference.
  439.      *
  440.      *******************************************/
  441.  
  442. difference_array(the_image, out_image, size)
  443.    int    size;
  444.    short  the_image[ROWS][COLS],
  445.           out_image[ROWS][COLS];
  446. {
  447.    int i, j, sd2;
  448.    sd2   = size/2;
  449.  
  450.    for(i=sd2; i<ROWS-sd2; i++){
  451.       for(j=sd2; j<COLS-sd2; j++){
  452.          out_image[i][j] =
  453.             abs(the_image[i][j] - 
  454.                 the_image[i+sd2][j+sd2]);
  455.       }  /* ends loop over j */
  456.    }  /* ends loop over i */
  457.  
  458.    fix_edges(out_image, sd2);
  459.  
  460. }  /* ends difference_array */
  461.  
  462.  
  463.  
  464.      /*******************************************
  465.      *
  466.      *   hurst(..
  467.      *
  468.      *   This routine performs the Hurst 
  469.      *   operation as described in "The Image 
  470.      *   Processing Handbook" by John C. Russ
  471.      *   CRC Press 1992.
  472.      *
  473.      *   The following show the definitions of
  474.      *   the pixel classes used in this routine.
  475.      *
  476.      *   3x3 case
  477.      *       c b c
  478.      *     d b a b d
  479.      *       c b c
  480.      *
  481.      *   5x5 case
  482.      *     f e d e f
  483.      *     e c b c e
  484.      *     d b a b d
  485.      *     e c b c e
  486.      *     f e d e f
  487.      *
  488.      *   7x7 case
  489.      *       h g h
  490.      *     f e d e f
  491.      *   h e c b c e h
  492.      *   g d b a b d g
  493.      *   h e c b c e h
  494.      *     f e d e f
  495.      *       h g h
  496.      *
  497.      *******************************************/
  498.  
  499. hurst(in_name, out_name, the_image, out_image,
  500.       il, ie, ll, le, size)
  501.    char   in_name[], out_name[];
  502.    int    il, ie, ll, le, size;
  503.    short  the_image[ROWS][COLS],
  504.           out_image[ROWS][COLS];
  505.  
  506. {
  507.    float  x[8], y[8], sig[8];
  508.    float  aa, bb, siga, sigb, chi2, q;
  509.    int    ndata, mwt;
  510.  
  511.    int    a, b, count, i, j, k,
  512.           new_hi, new_low, length,
  513.           number, sd2, sd2p1, ss, width;
  514.    short  *elements, max, prange;
  515.    struct tiff_header_struct image_header;
  516.  
  517.       /**********************************************
  518.       *
  519.       *   Initialize the ln's of the distances.
  520.       *   Do this one time to save computations.
  521.       *
  522.       **********************************************/
  523.  
  524.    x[1] = 0.0;        /* ln(1)        */
  525.    x[2] = 0.34657359; /* ln(sqrt(2))  */
  526.    x[3] = 0.69314718; /* ln(2)        */
  527.    x[4] = 0.80471896; /* ln(sqrt(5))  */
  528.    x[5] = 1.03972077; /* ln(sqrt(8))  */
  529.    x[6] = 1.09861229; /* ln(3)        */
  530.    x[7] = 1.15129255; /* ln(sqrt(10)) */
  531.  
  532.    sig[1] = 1.0;
  533.    sig[2] = 1.0;
  534.    sig[3] = 1.0;
  535.    sig[4] = 1.0;
  536.    sig[5] = 1.0;
  537.    sig[6] = 1.0;
  538.    sig[7] = 1.0;
  539.  
  540.    sd2 = size/2;
  541.  
  542.       /**********************************
  543.       *
  544.       *   Create out file and read
  545.       *   input file.
  546.       *
  547.       ***********************************/
  548.  
  549.    create_file_if_needed(in_name, out_name, out_image);
  550.  
  551.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  552.  
  553.    max = 255;
  554.    if(image_header.bits_per_pixel == 4){
  555.       max = 16;
  556.    }
  557.  
  558.       /***************************
  559.       *
  560.       *   Loop over image array
  561.       *
  562.       ****************************/
  563.  
  564.    printf("\n");
  565.    for(i=sd2; i<ROWS-sd2; i++){
  566.       if( (i%2) == 0) printf("%d ", i);
  567.       for(j=sd2; j<COLS-sd2; j++){
  568.  
  569.          for(k=1; k<=7; k++) y[k] = 0.0;
  570.  
  571.             /*************************************
  572.             *
  573.             *   Go through each pixel class, set
  574.             *   the elements array, sort it, get
  575.             *   the range, and take the ln of the
  576.             *   range.
  577.             *
  578.             *************************************/
  579.  
  580.             /* b pixel class */
  581.          number      = 4;
  582.          elements    = (short *)
  583.                        malloc(number * sizeof(short));
  584.          elements[0] = the_image[i-1][j];
  585.          elements[1] = the_image[i+1][j];
  586.          elements[2] = the_image[i][j-1];
  587.          elements[3] = the_image[i][j+1];
  588.          sort_elements(elements, &number);
  589.          prange = elements[number-1] - elements[0];
  590.          if(prange < 0)  prange = prange*(-1);
  591.          if(prange == 0) prange = 1;
  592.          y[1] = log(prange);
  593.  
  594.             /* c pixel class */
  595.          elements[0] = the_image[i-1][j-1];
  596.          elements[1] = the_image[i+1][j+1];
  597.          elements[2] = the_image[i+1][j-1];
  598.          elements[3] = the_image[i-1][j+1];
  599.          sort_elements(elements, &number);
  600.          prange = elements[number-1] - elements[0];
  601.          if(prange < 0)  prange = prange*(-1);
  602.          if(prange == 0) prange = 1;
  603.          y[2] = log(prange);
  604.  
  605.             /* d pixel class */
  606.          elements[0] = the_image[i-2][j];
  607.          elements[1] = the_image[i+2][j];
  608.          elements[2] = the_image[i][j-2];
  609.          elements[3] = the_image[i][j+2];
  610.          sort_elements(elements, &number);
  611.          prange = elements[number-1] - elements[0];
  612.          if(prange < 0)  prange = prange*(-1);
  613.          if(prange == 0) prange = 1;
  614.          y[3] = log(prange);
  615.  
  616.             /* f pixel class */
  617.          if(size == 5  ||  size == 7){
  618.          elements[0] = the_image[i-2][j-2];
  619.          elements[1] = the_image[i+2][j+2];
  620.          elements[2] = the_image[i+2][j-2];
  621.          elements[3] = the_image[i-2][j+2];
  622.          sort_elements(elements, &number);
  623.          prange = elements[number-1] - elements[0];
  624.          if(prange < 0)  prange = prange*(-1);
  625.          if(prange == 0) prange = 1;
  626.          y[5] = log(prange);
  627.          }  /* ends if size == 5 */
  628.  
  629.             /* g pixel class */
  630.          if(size == 7){
  631.          elements[0] = the_image[i-3][j];
  632.          elements[1] = the_image[i+3][j];
  633.          elements[2] = the_image[i][j-3];
  634.          elements[3] = the_image[i][j+3];
  635.          sort_elements(elements, &number);
  636.          prange = elements[number-1] - elements[0];
  637.          if(prange < 0)  prange = prange*(-1);
  638.          if(prange == 0) prange = 1;
  639.          y[6] = log(prange);
  640.          }  /* ends if size == 7 */
  641.  
  642.          free(elements);
  643.  
  644.             /* e pixel class */
  645.          if(size == 5  ||  size == 7){
  646.          number      = 8;
  647.          elements    = (short *)
  648.                        malloc(number * sizeof(short));
  649.          elements[0] = the_image[i-1][j-2];
  650.          elements[1] = the_image[i-2][j-1];
  651.          elements[2] = the_image[i-2][j+1];
  652.          elements[3] = the_image[i-1][j+2];
  653.          elements[4] = the_image[i+1][j+2];
  654.          elements[5] = the_image[i+2][j+1];
  655.          elements[6] = the_image[i+2][j-1];
  656.          elements[7] = the_image[i+1][j-2];
  657.          sort_elements(elements, &number);
  658.          prange = elements[number-1] - elements[0];
  659.          if(prange < 0)  prange = prange*(-1);
  660.          if(prange == 0) prange = 1;
  661.          y[4] = log(prange);
  662.          }  /* ends if size == 5 */
  663.  
  664.             /* h pixel class */
  665.          if(size == 7){
  666.          elements[0] = the_image[i-1][j-3];
  667.          elements[1] = the_image[i-3][j-1];
  668.          elements[2] = the_image[i-3][j+1];
  669.          elements[3] = the_image[i-1][j+3];
  670.          elements[4] = the_image[i+1][j+3];
  671.          elements[5] = the_image[i+3][j+1];
  672.          elements[6] = the_image[i+3][j-1];
  673.          elements[7] = the_image[i+1][j-3];
  674.          sort_elements(elements, &number);
  675.          prange = elements[number-1] - elements[0];
  676.          if(prange < 0)  prange = prange*(-1);
  677.          if(prange == 0) prange = 1;
  678.          y[7] = log(prange);
  679.          }  /* ends if size == 7 */
  680.  
  681.          free(elements);
  682.  
  683.             /*************************************
  684.             *
  685.             *   Call the fit routine to fit the
  686.             *   data to a straight line. y=mx+b
  687.             *   The answer you want is the slope
  688.             *   of the line.  That is returned
  689.             *   in the parameter bb.
  690.             *
  691.             *************************************/
  692.          ndata = size;
  693.          mwt   = 1;
  694.          fit(x, y, ndata, sig, mwt, &aa, &bb,
  695.              &siga, &sigb, &chi2, &q);
  696.  
  697.          out_image[i][j] = (short)(bb*64.0);
  698.          if(out_image[i][j] > max)
  699.             out_image[i][j] = max;
  700.          if(out_image[i][j] < 0)
  701.             out_image[i][j] = 0;
  702.  
  703.       }  /* ends loop over j */
  704.    }  /* ends loop over i */
  705.  
  706.  
  707.    fix_edges(out_image, sd2);
  708.  
  709.    write_array_into_tiff_image(out_name, out_image,
  710.                                il, ie, ll, le);
  711.  
  712. }  /* ends hurst */
  713.  
  714.  
  715.  
  716.  
  717.  
  718.      /*******************************************
  719.      *
  720.      *   compare(..
  721.      *
  722.      *   This function compares a sizeXsize area
  723.      *   starting at line,element in an image
  724.      *   with all the sizeXsize areas in the
  725.      *   image.
  726.      *
  727.      *******************************************/
  728.  
  729. compare(in_name, out_name, the_image, out_image,
  730.           il, ie, ll, le, line, element, size)
  731.    char   in_name[], out_name[];
  732.    int    il, ie, ll, le,
  733.           line, element, size;
  734.    short  the_image[ROWS][COLS],
  735.           out_image[ROWS][COLS];
  736. {
  737.    int      a, b, count, i, j, k, max,
  738.             sd2, sd2p1;
  739.    short    pixel;
  740.    struct   tiff_header_struct image_header;
  741.    int      big, diff;
  742.  
  743.       /**************************************
  744.       *
  745.       *   Declare and allocate memory for the
  746.       *   two dimensional small array.
  747.       *
  748.       ***************************************/
  749.  
  750.    short **small;
  751.    small = malloc(size * sizeof(short  *));
  752.    for(i=0; i<size; i++){
  753.       small[i] = malloc(size * sizeof(short ));
  754.       if(small[i] == '\0'){
  755.          printf("\n\tmalloc of small[%d] failed", i);
  756.          exit(0);
  757.       }
  758.    }
  759.  
  760.       /**************************************
  761.       *
  762.       *   Read in the part of the input image
  763.       *   that contains the line element
  764.       *   portion.
  765.       *
  766.       ***************************************/
  767.  
  768.    read_tiff_image(in_name, the_image,
  769.                    line, element,
  770.                    line+size, element+size);
  771.  
  772.    for(i=0; i<size; i++)
  773.       for(j=0; j<size; j++)
  774.         small[i][j] = the_image[i][j];
  775.  
  776.       /**************************************
  777.       *
  778.       *   Create the output image and read
  779.       *   in the input data.
  780.       *
  781.       ***************************************/
  782.  
  783.    sd2   = size/2;
  784.    sd2p1 = sd2 + 1;
  785.  
  786.    create_file_if_needed(in_name, out_name, out_image);
  787.  
  788.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  789.  
  790.    max = 255;
  791.    if(image_header.bits_per_pixel == 4){
  792.        max = 16;
  793.    }
  794.  
  795.       /**************************************
  796.       *
  797.       *   Loop over the image array and
  798.       *   calculate the contrast measure.
  799.       *
  800.       ***************************************/
  801.  
  802.    printf("\n");
  803.    for(i=sd2; i<ROWS-sd2; i++){
  804.       if( (i%10) == 0) printf("%d ", i);
  805.       for(j=sd2; j<COLS-sd2; j++){
  806.  
  807.          big = 0;
  808.          for(a=-sd2; a<sd2p1; a++){
  809.             for(b=-sd2; b<sd2p1; b++){
  810.             diff = small[a+sd2][b+sd2] -
  811.                    the_image[i+a][j+b];
  812.             big  = big + abs(diff);
  813.             }
  814.          }
  815.  
  816.       big = big/(size*size);
  817.       out_image[i][j] = big;
  818.       if(out_image[i][j] > max)
  819.          out_image[i][j] = max;
  820.  
  821.       }  /* ends loop over j */
  822.    }  /* ends loop over i */
  823.  
  824.    fix_edges(out_image, sd2);
  825.  
  826.    write_array_into_tiff_image(out_name, out_image,
  827.                                il, ie, ll, le);
  828.  
  829.       /**************************************
  830.       *
  831.       *   Free the memory for the
  832.       *   two dimensional small array.
  833.       *
  834.       ***************************************/
  835.  
  836.    for(i=0; i<size; i++)
  837.       free(small[i]);
  838.  
  839. }  /* ends compare */
  840.  
  841.  
  842.  
  843.  
  844.  
  845.      /*******************************************
  846.      *
  847.      *   get_texture_options(..
  848.      *
  849.      *   This function queries the user for the
  850.      *   parameters needed to use the texture
  851.      *   operators.
  852.      *
  853.      *******************************************/
  854.  
  855. get_texture_options(type, threshold, t_value, size, 
  856.                     line, element)
  857.    char type[];
  858.    int  *threshold, *t_value, *size, *line, *element;
  859. {
  860.    int not_finished = 1, response;
  861.  
  862.    while(not_finished){
  863.  
  864.       printf("\n");
  865.       printf("\n1. Type of texture operator is %s",
  866.              type);  
  867.       printf("\n   recall type: sigma      skewness");
  868.       printf("\n                amean      adifference");
  869.       printf("\n                hurst      compare");
  870.       printf("\n2. threshold is %d (1=on 2=off)",
  871.              *threshold);
  872.       printf("\n3. threshold value is %d", *t_value);
  873.       printf("\n4. size is %d", *size);
  874.       printf("\n5. line is %d", *line);
  875.       printf("\n6. element is %d\n", *element);
  876.       printf("\n   usage for compare:");
  877.       printf("\n   texture in-file out-file compare ");
  878.       printf("line element size");
  879.       printf("\n\nEnter choice (0 = no change) _\b");
  880.  
  881.       get_integer(&response);
  882.  
  883.       if(response == 0){
  884.         not_finished = 0;
  885.       }
  886.  
  887.       if(response == 1){
  888.          printf("\nEnter type of texture operator");
  889.          printf("\nrecall type: sigma  skewness");
  890.          printf("\n             amean  adifference");
  891.          printf("\n             hurst  compare\n");
  892.          gets(type);
  893.       }
  894.  
  895.       if(response == 2){
  896.         printf("\n\nEnter threshold output ");
  897.         printf("(1=on 2=off)");
  898.         printf("\n  _\b");
  899.         get_integer(threshold);
  900.       }
  901.  
  902.       if(response == 3){
  903.         printf("\n\nEnter threshold value");
  904.         printf("\n  _\b");
  905.         get_integer(t_value);
  906.       }
  907.  
  908.       if(response == 4){
  909.         printf("\n\nEnter size");
  910.         printf("\n  _\b");
  911.         get_integer(size);
  912.       }
  913.  
  914.       if(response == 5){
  915.         printf("\n\nEnter line");
  916.         printf("\n  _\b");
  917.         get_integer(line);
  918.       }
  919.  
  920.       if(response == 6){
  921.         printf("\n\nEnter element");
  922.         printf("\n  _\b");
  923.         get_integer(element);
  924.       }
  925.  
  926.    }  /* ends while not_finished */
  927. }  /* ends get_texture_options */
  928.